<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/22 18:01
 */

namespace app\admin\controller;

use app\admin\business\AdminAuth as AdminAuthBus;
use app\admin\business\AdminRole as AdminRoleBus;
use app\common\lib\ArrayLib;
use app\common\lib\File;
use app\common\lib\Show;
use think\facade\Cache;
use think\facade\View;

class Index extends AdminBase {

    public function index() {
        return View::fetch('', [
            'admin' => $this->adminUser
        ]);
    }

    public function welcome() {
        return View::fetch();
    }

    // 菜单初始化接口
    public function initMenu(){
        $homeInfo = [
            'title' => '首页',
            'href'  => "/admin/index/welcome.html",
        ];

        $logoInfo = [
            'title' => 'TanAdmin',
            'image' => '/layuimini/images/logo.png',
            'href'  => '',
        ];

        $role = (new AdminRoleBus())->getByIds(explode(',', $this->adminUser['role_id']));
        $rules = ArrayLib::getPushStringToArray($role);
        $menu = (new AdminAuthBus())->getAuthMenuLists(['type' => 1, 'is_hidden' => 0], $rules);
        $menuInfo = ArrayLib::getTree($menu, 'child');

        $result = ['homeInfo' => $homeInfo, 'logoInfo' => $logoInfo, 'menuInfo' => $menuInfo];

        return json($result);
    }

    //缓存更新
    public function cache()
    {
        $type = $this->request->request("type");
        switch ($type) {
            case 'data' || 'all':
                File::del_dir(ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR . 'admin/cache');
                Cache::clear();
                if ($type == 'content') {
                    break;
                }

            case 'template' || 'all':
                File::del_dir(ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR . 'admin/temp');
                if ($type == 'template') {
                    break;
                }
        }
            return Show::success('服务端清理缓存成功');
    }

    // 退出登录
    public function logout() {
        session(config('admin.session_admin'), null);
        return redirect((string)url('login/index'));
    }

}